import { getBehaviourFactor } from '@/api/icp/labelClassDisplay'
// 行为改变
export async function changeAction(val, index, arr, analyseType) {
  await getBehaviourFactor(val).then((res) => {
    if (arr.cascadeList.length > 0) {
      // 级联选择器
      arr.cascadeList[index].list = res.censusList.map((item) => {
        return {
          label: item.value + ' ( ' + item.key + ' ) ',
          value: item.key,
          children:
            item.children &&
            item.children.map((i) => {
              return {
                label: i.value + ' ( ' + i.key + ' ) ',
                value: i.key
              }
            })
        }
      })
    }

    arr.actionFirstList[index].list = res.itemList.map((item) => {
      return {
        name: item.fieldName,
        value: item.propertyDesc,
        label: item.fieldName + ' ( ' + item.propertyDesc + ' ) ',
        propId: item.id,
        type: item.fieldType,
        isEnum: item.enumStatus,
        enumList: item.dictList,
        groupType: item.groupType
      }
    })
    if (analyseType && analyseType === 'batch') {
      arr.groupList[index].list = res.groupList.map((item) => {
        return {
          value: item.fieldName,
          label: item.fieldAlias + ' ( ' + item.fieldName + ' ) '
        }
      })
    } else {
      arr.groupList = res.groupList.map((item) => {
        return {
          value: item.fieldName,
          label: item.fieldAlias + ' ( ' + item.fieldName + ' ) '
        }
      })
    }
  })
}
// 处理行为第一步数据
export function formatting(val, arr, it, typevalue, index, idx, formArr) {
  if (val) {
    const types = arr.filter((i) => {
      return i.value === val
    })
    it.isEnum = types[0].isEnum
    it.valueType = types[0].type
    it.type = typevalue

    debugger
    if (typevalue === 'profile') {
      // 判断是否可枚举
      if (it.isEnum == 0) {
        formArr.secondList[index].lists[idx].list =
         formArr.typeOptions.filter((ii) => {
           return ii.type === it.valueType
         })
      } else {
        formArr.secondList[index].lists[idx].list =
         formArr.typeOptions.filter((ii) => {
           return ii.type === 'json'
         })

        it.useType = 'enum'
        // 获取第三个列表值
        formArr.enumList[index].lists[idx].list =
          types[0].enumList.map((s) => {
            return {
              value: s.value,
              label: s.name,
              propId: s.propId,
              id: s.id,
              type: s.type
            }
          })
      }
    } else {
      // 判断是否可枚举
      if (it.isEnum == 0) {
        formArr.actionSecondList[index].lists[idx].list =
         formArr.typeOptions.filter((ii) => {
           return ii.type === it.valueType
         })
      } else {
        formArr.actionSecondList[index].lists[idx].list =
         formArr.typeOptions.filter((ii) => {
           return ii.type === 'json'
         })
        it.useType = 'enum'
        // 获取第三个列表值
        formArr.actionEnumList[index].lists[idx].list =
          types[0].dictList.map((s) => {
            return {
              value: s.dictValue,
              label: s.dictKey,
              propId: s.groupId,
              id: s.id,
              type: s.type
            }
          })
      }
    }
  }
}
// 处理第二步数据
export function formatActSec(val, it, typevalue) {
  if (typeof val === 'object') return
  if (it.isEnum == 0) {
    if (typevalue === 'filter') {
      if (val === 'equal' || val === 'regex') {
        it.useType = 'input'
      } else if (val === 'in' || val === 'not_in') {
        it.useType = 'a-select'
      } else if (val === 'in_time_range') {
        it.useType = 'datetime'
      } else {
        it.useType = 'outer'
      }
    } else {
      if (val === 'equal' || val === 'regex') {
        it.useType = 'input'
      } else if (val === 'in' || val === 'not_in') {
        it.useType = 'a-select'
      } else {
        it.useType = 'outer'
      }
    }
  } else {
    if (val !== 'equal') {
      it.useType = ''
    } else {
      it.useType = 'normal'
    }
  }
}
//  修改实体属性第一行的值
export function confirmFirst(val, index, idx, formObj, formArr) {
  const it = formObj.property.conditions[index].conditions[idx]
  this.formatting(
    val,
    formArr.attrList,
    it,
    'profile',
    index,
    idx
  )
  it.operator = ''
  it.param = undefined
}
// 修改属性第二项值
export function confirmSec(val, index, idx, formObj) {
  const it = formObj.property.conditions[index].conditions[idx]
  this.formatActSec(val, it, 'profile')

  if (val === 'in' || val === 'not_in') {
    it.param = []
  } else {
    it.param = ''
  }
}
// 修改实体行为组第一项值
export function confirmActFirst(val, index, idx, formObj, formArr) {
  debugger
  const it = formObj.action.conditions[index].conditions[idx]
  this.formatting(
    val,
    formArr.actionFirstList[index].list,
    it,
    'filter',
    index,
    idx
  )
  it.operator = ''
  it.param = undefined
  it.refResource = ''
}
// 修改实体行为组第2项值
export function confirmActSec(val, index, idx, formObj) {
  const it = formObj.action.conditions[index].conditions[idx]

  this.formatActSec(val, it, 'filter')
  if (val === 'in' || val === 'not_in') {
    it.param = []
  } else {
    it.param = ''
  }
  it.refResource = ''
}
// 最后是计数器还是选择资源表
export function confirmOperate(val, index, formObj) {
  if (val.indexOf('ref_') !== -1) {
    formObj.action.conditions[index].useType = 'outer'
  }
}
